package com.newer.controller.sys;

import java.io.File;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.newer.pojo.sys.Region;
import com.newer.service.sys.iter.RegionService;
import com.newer.utils.sys.BootstrapDataTable;
import com.newer.utils.sys.Conditions;
import com.newer.utils.sys.MsgUtil;
import com.newer.utils.sys.PinYin4jUtils;

/**
 * 区域管理控制层
 * 
 * @author ShuFan
 */
@Controller
@RequestMapping(value = { "region" })
public class RegionController {

	@Autowired
	private RegionService regionService;

	/**
	 * 根据id查询区域信息 跳转到区域编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = { "selectRegionById/{id}" })
	public String selectById(@PathVariable("id") String id, HttpServletRequest request) {
		Region region = regionService.selectByPrimaryKey(id);
		request.setAttribute("region", region);
		return "sys/region/region_edit";
	}

	/**
	 * 添加一个区域信息
	 * 
	 * @param region
	 * @return
	 */
	@RequestMapping(value = { "addRegion" })
	public @ResponseBody MsgUtil addRegion(Region region) {
		System.out.println("添加区域信息");
		// 设置id的值
		region.setId(UUID.randomUUID().toString());
		// 设置城市编码
		region.setCityCode(PinYin4jUtils.hanziToPinyin(region.getCity(), ""));
		// 设置简码
		String[] shortCode = PinYin4jUtils.getHeadByString(region.getCity() + region.getDistrict());
		region.setShortCode(StringUtils.join(shortCode));
		// 插入数据
		int i = regionService.insertSelective(region);
		if (i != 0) {
			return MsgUtil.result(true, "添加成功");
		} else {
			return MsgUtil.result(false, "添加失败");
		}
	}

	/**
	 * 查询所有的区域信息
	 * 
	 * @param dataTable
	 * @param conditions
	 * @return
	 */
	@RequestMapping(value = { "regionList" })
	public @ResponseBody BootstrapDataTable regionList(BootstrapDataTable dataTable, Conditions conditions) {
		BootstrapDataTable regionByExample = regionService.selectRegionByExample(dataTable, conditions);
		return regionByExample;
	}

	/**
	 * 根据id修改一条区域信息
	 * 
	 * @param region
	 * @return
	 */
	@RequestMapping(value = { "updataRegion" })
	public @ResponseBody MsgUtil updataRegion(Region region) {
		// 设置城市编码
		region.setCityCode(PinYin4jUtils.hanziToPinyin(region.getCity(), ""));
		// 设置简码
		String[] shortCode = PinYin4jUtils.getHeadByString(region.getCity() + region.getDistrict());
		region.setShortCode(StringUtils.join(shortCode));
		int i = regionService.updateByPrimaryKeySelective(region);
		if (i != 0) {
			return MsgUtil.result(true, "修改成功");
		} else {
			return MsgUtil.result(false, "修改失败");
		}
	}

	/**
	 * 根据id删除一条区域信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = { "deleteRegion" })
	public @ResponseBody MsgUtil deleteRegion(String id) {
		int i = regionService.deleteByPrimaryKey(id);
		if (i != 0) {
			return MsgUtil.result(true, "删除成功");
		} else {
			return MsgUtil.result(false, "删除失败");
		}
	}

	/**
	 * 批量删除区域信息
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = { "deleteRegionByIds" })
	public @ResponseBody MsgUtil deleteRegionByIds(String ids[]) {
		int i = regionService.deleteRegionByIds(ids);
		if (i != 0) {
			return MsgUtil.result(true, "批量删除成功");
		} else {
			return MsgUtil.result(false, "批量删除失败");
		}
	}

	/**
	 * 批量导入区域信息
	 * @param fileName
	 * @param request
	 * @param response
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	@RequestMapping(value = { "importRegions" })
	public @ResponseBody MsgUtil importRegions(@RequestParam("fileName") CommonsMultipartFile fileName,HttpSession session) throws FileNotFoundException, IOException {
		System.out.println("批量导入数据方法");
		
		String string = regionService.insertRegions(fileName,session);
		if (string.equals("成功")) {
			return MsgUtil.result(true, "批量导入成功");
		} else {
			return MsgUtil.result(false, "批量导入失败");
		}
	}
}